#include "Generate_C_std.h"
#include "Pattern_assign_var.h"

/**
   template getstring
 */

void Print(String* pStr, ostream&os)
{
  os << "String(";
  if (pStr)
    {
      os << *pStr;
    }
  os << ")";
}


void   Print(MIR::VARIABLE_NAME*variable,ostream & os)
{
  os << "MyVariable(";
  Print(variable->value,os);
  os << ")";
}

void  Print(MIR::Class_name*class_name,ostream & os)
{
  os << "MyMIRClassName(";

  CLASS_NAME* pclass = dynamic_cast<CLASS_NAME*>(class_name);
  if (pclass)
    {
      Print(pclass->value,os);
    }
  os << ");";
}

void   Print(MIR::Instanceof* aInstanceof,ostream & os)
{
  os << "MyInstanceof(";
  Print(aInstanceof->variable_name,os);
  os << ",";
  Print(aInstanceof->class_name,os);
  os << ")";
}


class Pattern_assign_expr_typeof : public Pattern_assign_var
{
public:
	Expr* rhs_pattern()
	{
		rhs = new Wildcard<Instanceof>;
		return rhs;
	}

	void generate_code(Generate_C* gen)
	{
		CLASS_NAME* class_name;
		Variable_class* variable_class;

		Instanceof* nw = rhs->value;

		class_name = dynamic_cast<CLASS_NAME*>(nw->class_name);
		variable_class = dynamic_cast<Variable_class*>(nw->class_name);

		// TODO: call the constructor (or pass the arguments for the constructor
		// to the various templates)
		if(class_name != NULL)
		{
			if(!agn->is_ref)
				INST (buf, "assign_expr_new", lhs->value, class_name);
			else
				INST (buf, "assign_expr_new_ref", lhs->value, class_name);
		}
		else if(variable_class != NULL)
		{
			VARIABLE_NAME* vcn = variable_class->variable_name;

			if(!agn->is_ref)
			  {
			    buf << "assign_expr_instanceOf";
			    Print(lhs->value,buf);
			    buf << ",";
			    Print(vcn,buf);
			    buf << ");";
			  }
			else
			  {
			    buf << "assign_expr_instanceOfRef(";
			    Print(lhs->value,buf);
			    buf << ",";
			    Print(vcn,buf);
			    buf << ");";
			  }
		}
		else
		{
			// Invalid class name type
			phc_unreachable();
		}

    // See comment in expr_method_invocation
		/*		Object_list* params = new Object_list;
		foreach (Actual_parameter* ap, *rhs->value->actual_parameters)
		{
			params->push_back (ap->rvalue);
			if (ap->is_ref)
				ap->rvalue->attrs->set_true ("phc.codegen.is_ref");
				}*/


		/*INST (buf, "InstanceOf",
		      rhs->value->get_filename (),
		      s(lexical_cast<string>(rhs->value->get_line_number())),
		      lhs->value);*/
		
		


		buf << "MyTypeOf(";
		Print(rhs->value,buf);
		buf << ");\n";



	}

protected:
	Wildcard<Instanceof>* rhs;
};

Pattern * createPattern_Assign_var()
{
  return new Pattern_assign_expr_typeof();
}


/**
   just pass through this to a macro MyAssign_var
*/
/*
class Pattern_Assign_var : public Pattern
{
public:
	bool match(Statement* that)
	{
	  //Name_with_default* var

		variable_name = new Wildcard<Variable_variable>;
		expr =  new Wildcard<Expr>;
		return that->match(variable_name
				   //new
				   //Assign_var(variable_name,(Expr*)expr)
);
	}

	void generate_code(Generate_C* gen)
	{
	  String * pString= 
	    variable_name->variable_name->value;

	  //= variable_name->expr;

	  //MIR::VARIABLE_NAME
	  buf << "MyAssign_var(" << *pString << ");\n";
	}

protected:
  //MIR::Name_with_default*
  //  Wildcard<Variable_variable>* variable_name;
  Wildcard<Variable_variable>* variable_name;
  Wildcard<Expr> * expr; 

};
*/



/*
2009/01/php/phc-read-only/src/codegen/Generate_C.cpp

if( !$target instanceof Title ) {

Not yet supported in code generation: unknown construct
$TLE3364 = $target instanceof title;

<MIR:Assign_var>
	<attrs />
	<MIR:VARIABLE_NAME>
		<attrs>
			<attr key="phc.codegen.compiler_generated"><bool>true</bool></attr>
			<attr key="phc.codegen.st_entry_not_required"><bool>true</bool></attr>
		</attrs>
		<value>TLE3364</value>
	</MIR:VARIABLE_NAME>
	<bool><!-- is_ref -->false</bool>
	<MIR:Instanceof>
		<attrs>
			<attr key="phc.filename"><string>./Parser2.php</string></attr>
			<attr key="phc.line_number"><integer>1566</integer></attr>
		</attrs>
		<MIR:VARIABLE_NAME>
			<attrs>
				<attr key="phc.codegen.st_entry_not_required"><bool>true</bool></attr>
				<attr key="phc.filename"><string>./Parser2.php</string></attr>
				<attr key="phc.line_number"><integer>1566</integer></attr>
			</attrs>
			<value>target</value>
		</MIR:VARIABLE_NAME>
		<MIR:CLASS_NAME>
			<attrs />
			<value>title</value>
		</MIR:CLASS_NAME>
	</MIR:Instanceof>
</MIR:Assign_var>
*/
